import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {

    public static void reflectNewInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException {//调用该方法实现创建对象
        Class<?> c1 = Class.forName("Student");//在进行反射前需要先获取Class对象
        Student student = (Student) c1.newInstance();//强制转型--->拿到了Student对象
        System.out.println(student);
    }
    //反射私有构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("Student");
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);//设置为true,使得private的构造方法可以进行修改访问权限
            Student student = (Student) constructor.newInstance("lcc", 23);

            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    //反射私有属性
    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("Student");
            Student student = (Student) c1.newInstance();
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);//修改访问权限之后才能进行内容的修改
            field.set(student,"xkx");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

    }
    //反射私有方法
    public static void reflectPrivateMethod(){
        try {
            Class<?> c1 = Class.forName("Student");//获取Class对象
            Student student = (Student) c1.newInstance();//实例化对象并进行强制类型转换
            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            method.invoke(student,"我是一个参数");//使用对象的私有方法
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectPrivateMethod();
    }
    public static void main3(String[] args) {
        reflectPrivateField();
    }
    public static void main2(String[] args) {
        reflectPrivateConstructor();
    }
    public static void main1(String[] args){
        try {
            reflectNewInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
